﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using MySite.Model;

namespace MySite.Helper
{
    public class FileUpload
    {
        public static string BasePath = "DownLoad\\";
        public static string DayPath = BasePath +DateTime.Now.ToString("yyyy-MM");
        public static string DayFullPath = HttpRuntime.AppDomainAppPath + BasePath + DateTime.Now.ToString("yyyy-MM");
        public static string FullBasePath = HttpRuntime.AppDomainAppPath + BasePath;

        #region 获取默认路径下的文件名称
        /// <summary>
        /// 获取默认路径下的文件名称
        /// </summary>
        /// <returns></returns>
        public static IList<string> GetFiles()
        {
            List<string> result = new List<string>();

            string[] dirs = Directory.GetDirectories(FullBasePath);

            foreach (string dir in dirs)
            {
                //如果有这个目录,把目录里的内容列出来
                if (!dir.Contains(".svn") && Directory.Exists(dir))
                {
                    result.AddRange(Directory.GetFiles(dir));

                }
            }

            for (int i = 0; i < result.Count; i++)
            {
                result[i] = result[i].Replace(HttpRuntime.AppDomainAppPath, "../").Replace("\\", "/");
            }

            return result;
        }
        #endregion

        #region 获取指定路径下的文件名称
        /// <summary>
        /// 获取指定路径下的文件名称
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static IList<string> GetFiles(string path)
        {
            List<string> result = new List<string>();

            string[] dirs = Directory.GetDirectories(FullBasePath);

            foreach (string dir in dirs)
            {
                //如果有这个目录,把目录里的内容列出来
                if (!dir.Contains(".svn") && Directory.Exists(dir))
                {
                    result.AddRange(Directory.GetFiles(dir));

                }
            }

            for (int i = 0; i < result.Count; i++)
            {
                result[i] = result[i].Replace(HttpRuntime.AppDomainAppPath, "../").Replace("\\", "/");
            }

            return result;
        }
        #endregion

        #region 上传文件(原文件名称)
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static ReturnMessage Upload(HttpPostedFile file)
        {
            ReturnMessage rm = new ReturnMessage();

            try
            {
                if (file != null)
                {
                    if (!System.IO.Directory.Exists(DayFullPath))
                    {
                        System.IO.Directory.CreateDirectory(DayFullPath);
                    }

                    var filenames = file.FileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);

                    file.SaveAs(DayFullPath + "\\" + filenames[filenames.Length - 1]);
                    rm.Text = "../../" + DayFullPath.Replace("\\", "/") + "/" + filenames[filenames.Length - 1];
                    rm.IsSuccessed = true;
                }
            }
            catch
            {
                rm.IsSuccessed = false;
            }

            return rm;
        }
        #endregion

        #region 上传文件,指定文件名称
        /// <summary>
        /// 上传文件,指定文件名称
        /// </summary>
        /// <param name="file"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ReturnMessage Upload(HttpPostedFile file, string fileName)
        {
            ReturnMessage rm = new ReturnMessage();

            try
            {
                if (file != null)
                {
                    if (!System.IO.Directory.Exists(DayFullPath))
                    {
                        System.IO.Directory.CreateDirectory(DayFullPath);
                    }

                    if (File.Exists(DayFullPath + "\\" + fileName))
                    {
                        File.Delete(DayFullPath + "\\" + fileName);
                    }

                    file.SaveAs(DayFullPath + "\\" + fileName);
                    rm.Text = "../../" + DayFullPath.Replace("\\", "/") + "/" + fileName;
                    rm.IsSuccessed = true;
                }
            }
            catch
            {
                rm.IsSuccessed = false;
            }

            return rm;
        }
        #endregion

        #region 上传文件,指定文件名称,指定路径
        /// <summary>
        /// 上传文件,指定文件名称,指定路径
        /// </summary>
        /// <param name="file"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ReturnMessage Upload(HttpPostedFile file, string fileName, string path)
        {
            ReturnMessage rm = new ReturnMessage();

            try
            {
                if (file != null)
                {
                    string subPath = path;

                    if (!System.IO.Directory.Exists(HttpRuntime.AppDomainAppPath + subPath))
                    {
                        System.IO.Directory.CreateDirectory(HttpRuntime.AppDomainAppPath + subPath);
                    }

                    if (File.Exists(HttpRuntime.AppDomainAppPath + subPath + "\\" + fileName))
                    {
                        File.Delete(HttpRuntime.AppDomainAppPath + subPath + "\\" + fileName);
                    }

                    file.SaveAs(HttpRuntime.AppDomainAppPath + subPath + "\\" + fileName);
                    rm.Text = "../../" + subPath.Replace("\\", "/") + "/" + fileName;
                    rm.IsSuccessed = true;
                }
            }
            catch
            {
                rm.IsSuccessed = false;
            }

            return rm;
        }
        #endregion
    }
}
